package com.illtamer.infinite.collapsar.common;

import com.mysql.cj.jdbc.MysqlDataSource;
import lombok.Getter;
import lombok.SneakyThrows;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class MySQLConfig {

    public static final String EMAIL_TABLE = "collapsar_email";

    public static final String USER_TABLE = "collapsar_email";

    @Getter
    private final DataSource dataSource;

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @SneakyThrows(SQLException.class)
    public MySQLConfig(String host, int port, String databaseName, String username, String password) {
        MysqlDataSource mysql = new MysqlDataSource();
        mysql.setServerName(host);
        mysql.setPort(port);
        mysql.setDatabaseName(databaseName);
        mysql.setUser(username);
        mysql.setPassword(password);
        mysql.getConnection();
        dataSource = mysql;
    }

    @SneakyThrows(SQLException.class)
    public Connection getConnection() {
        return dataSource.getConnection();
    }

}
